<?php
namespace app\api\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;

class InfoList extends Base{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        //栏目id
        $this->catid=Request::param("catid");
        $cat=Db::name("cat")->find($this->catid);
        if(!$cat){
            $this->error("栏目信息不存在");
        }
        $cat_data=Db::name("cat_data")->find($this->catid);
        $this->cat=array_merge($cat,$cat_data);
        //栏目字段赋值
        //当前栏目对应的模型信息
        $this->ModelInfo=$GLOBALS['model'][$this->cat['modelid']];
        $this->table="cms_".$this->ModelInfo['table_name'];
    }
    public function index()
    {
        $where=[];
        //分类
        $where[]=array('catid','in',getSonCat($this->catid));
        //关键词
        $keywords=Request::param("keywords");
        if($keywords){
            $where[]=['keywords|title|description','LIKE','%'.$keywords.'%'];
            View::assign("keywords",$keywords);
        }
        //按指定字段搜索
        $field=explode(",",trim($this->ModelInfo["is_search"],","));
        if($field){
            foreach($field as $v){
                if(!$v){continue;}
                if(Request::get($v)){
                    $getval=Request::get($v);
                    //判断查询条件(区间查询)
                    if(strstr($getval,"__")){
                        $getval_arr=explode("__",$getval);
                        //如果没有第一个参数，则认为是<于查询
                        if($getval_arr[0]===""){
                            $where[]=array($v,"<",$getval_arr[1]);
                        }
                        //如果没有第二个参数，则认为是>于查询
                        elseif($getval_arr[1]===""){
                            $where[]=array($v,">",$getval_arr[0]);
                        }else{
                            $where[]=array($v,"BETWEEN",array($getval_arr[0],$getval_arr[1]));
                        }
                    }elseif(strstr($getval,"%%")){
                        $trim_val=trim($getval,"%%");
                        $where[]=array($v,'LIKE','%'.$trim_val.'%');
                        $_GET[$v]=$trim_val;
                    }else{
                        $where[$v]=$getval;
                    }
                }
            }
        }
        $this->where=$where;//这里不能删除,这里是用于模板里筛选的
        //排序方式
        $order=$this->cat["listorder"]?$this->cat["listorder"]:"sort asc,is_top desc,is_good desc,newstime desc";//默认排序方式
        $field=explode(",",trim($this->ModelInfo["is_sort"],","));
        if($field){
            foreach($field as $v){
                if(Request::param("order")&&Request::param("order")==$v){
                    $ordertype=Request::param("ordertype");
                    //正序
                    if($ordertype){
                        $order=$v." asc";
                    }
                    //倒充
                    else{
                        $order=$v." desc";
                    }
                }
            }
        }
        //查询指定字段
        $field="id,catid,modelid,titleurl,pagetitle,keywords,description,pubid,sort,userid,username,addtime,editortime,is_good,is_top,views,goods,comments";
        $field=$field.$this->ModelInfo["is_list"];
        $field=trim($field,",");
        //查询与分页
        $this->app->bind('think\Paginator' , 'app\common\page\ListPage');//单独定义列表分页
        $listRows=Request::param("listRow")?Request::param("listRow"):$this->cat['lencord'];//每页显示多少条
        $total=Db::name($this->table)->where($where)->count();
        $data=Db::name($this->table)->where($where)->order($order)->paginate(['list_rows'=>$listRows,'query' => request()->param()]);
        //
        $newdata=[];
        foreach($data as $v){
            $v["titleurl"]=titleurl($v);
            $v["cat"]=Db::name("cat")->find($v['catid']);
            $newdata[]=$v;
        }
        $this->success([
            "data"=>$newdata,
            "total"=>$total,
        ]);

    }

}
